<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>建立索引</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.multi.query.html">获取一个子集</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.readpreferences.html">Read Preferences</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.indexes" class="section">
   <h2 class="title">建立索引</h2>
   <p class="para">
    MongoDB 支持索引，并且很容易给集合添加索引。你需要指定字段名和排序方向：
    升序（1）或降序（-1）。例如：
   </p>
   <div class="example" id="mongo.tutorial.indexes-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$coll</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ensureIndex</span><span style="color: #007700">(&nbsp;array(&nbsp;</span><span style="color: #DD0000">"i"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">)&nbsp;);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;create&nbsp;index&nbsp;on&nbsp;"i"<br /></span><span style="color: #0000BB">$coll</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ensureIndex</span><span style="color: #007700">(&nbsp;array(&nbsp;</span><span style="color: #DD0000">"i"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"j"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">)&nbsp;);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;index&nbsp;on&nbsp;"i"&nbsp;descending,&nbsp;"j"&nbsp;ascending<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <p class="para">
    当你的数据量变得越来越大，创建索引对读取性能有巨大的提升。如果你不了解索引，可以参考
    <span class="function"><a href="mongocollection.ensureindex.html" class="function">MongoCollection::ensureIndex()</a></span> 文档和 MongoDB 的
    <a href="http://docs.mongodb.org/manual/indexes/" class="link external">&raquo;&nbsp;MongoDB indexing documentation</a>。
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.multi.query.html">获取一个子集</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.readpreferences.html">Read Preferences</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
